package com.pg.agent.manager.controller;

import com.alibaba.fastjson.JSON;
import com.pg.agent.manager.model.dto.agent.AgentDTO;
import com.pg.agent.manager.model.dto.agent.AgentPageReq;
import com.pg.agent.manager.model.dto.agent.AgentVO;
import com.pg.agent.manager.service.AgentService;
import com.pg.base.component.IdDTO;
import com.pg.base.component.PageVO;
import com.pg.base.component.valid.Add;
import com.pg.base.component.valid.Update;
import com.pg.base.util.LoginUtils;
import com.pg.base.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 智能体应用
 *
 * @author admin
 * @date 2023-12-20 19:12:26
 */
@Slf4j
@RestController
@RequestMapping("/agent")
public class AgentController {

    @Resource
    private AgentService agentService;


    /**
     * 1.2已修改
     *
     * @param pageReq
     * @return
     */
    @GetMapping("page")
    public ResultVO<PageVO<AgentVO>> page(AgentPageReq pageReq) {
        log.info("receive agent page data=[{}]", JSON.toJSONString(pageReq));
        pageReq.setBelongId(LoginUtils.getLoginUser().getTenantId());
        PageVO<AgentVO> pageVO = agentService.page(pageReq);
        return ResultVO.instance(pageVO);
    }

    @GetMapping("get")
    public ResultVO<AgentVO> get(Long agentId) {
        log.info("receive agent get data=[{}]", agentId);
        AgentVO agentVO = agentService.get(agentId);
        return ResultVO.instance(agentVO);
    }


    @PostMapping("add")
    public ResultVO<Long> add(@RequestBody @Validated(Add.class) AgentDTO agentDTO) {
        log.info("receive agent add data=[{}]", JSON.toJSONString(agentDTO));
        Long id = agentService.add(agentDTO);

        return ResultVO.instance(id);
    }

    @PostMapping("update")
    public ResultVO<Integer> update(@RequestBody @Validated(Update.class)  AgentDTO agentDTO) {
        log.info("receive agent update data=[{}]", JSON.toJSONString(agentDTO));
        Integer num = agentService.update(agentDTO);
        return ResultVO.instance(num);
    }

    @PostMapping("delete")
    public ResultVO<Integer> delete(@RequestBody @Validated IdDTO idDto) {
        log.info("receive agent delete data=[{}]", idDto);
        Integer num =  agentService.deleteById(idDto.getId());
        return ResultVO.instance(num);
    }

}
